"""
Case Type   : bugfix
Case Name   : 兼容B库下，启用dolphin.b_compatibility_mode参数，普通用户查询权限验证
Create At   : 2025/6/12
Owner       : liu-tong-8848
Description :
    1、创建schema
    2、更改步骤1创建的schema为当前schema；
    3、创建普通用户
    4、把当前会话里的会话用户标识和当前用户表示都设置为步骤3创建的普通用户
    5、启用dolphin.b_compatibility_mode；
    6、执行简单查询，是否有权限限制
    7、重入会话，清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、成功
    6、成功
    7、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbBmode')
        self.user_node = Node('PrimaryDbBmode')
        self.constant = Constant()

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:创建schema；expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop schema if exists s_fixed_113; " \
                  + f"create schema s_fixed_113; " \
                  + f"set current_schema to s_fixed_113; " \
                  + f"drop user if exists u_fixed_113; " \
                  + f"create user u_fixed_113 password 'Test@123'; " \
                  + f"set session authorization u_fixed_113 password 'Test@123'; " \
                  + f"show dolphin.b_compatibility_mode; " \
                  + f"set dolphin.b_compatibility_mode to on; " \
                  + f"select 1+1;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertIn(self.constant.CREATE_SCHEMA_SUCCESS_MSG, sql_res, '执行失败' + text)

        text = '----step2:更改步骤1创建的schema为当前schema；expect:成功----'
        self.log.info(text)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)

        text = '----step3:创建普通用户；expect:成功----'
        self.log.info(text)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, sql_res, '执行失败' + text)

        text = '----step4:把当前会话里的会话用户标识和当前用户表示都设置为步骤3创建的普通用户；expect:成功----'
        self.log.info(text)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)

        text = '----step5:启用dolphin.b_compatibility_mode；expect:成功----'
        self.log.info(text)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)

        text = '----step6:执行简单查询，是否有权限限制；expect:成功----'
        self.log.info(text)
        self.assertIn('2', sql_res.strip().splitlines()[-2], '执行失败' + text)
        self.assertNotIn('ERROR', sql_res, '执行失败' + text)

    def tearDown(self):
        text = '----step7:重入会话，清理环境；expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop schema if exists s_fixed_113; " \
                  + f"drop user if exists u_fixed_113; " \
                  + f"set dolphin.b_compatibility_mode to off;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG, sql_res.strip(), '执行失败' + text)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, sql_res.strip(), '执行失败' + text)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res.strip(), '执行失败' + text)
        self.log.info(f'----{os.path.basename(__file__)} end-----')
